package com.gimi.cloud.jimi.manage.consumer.service.prod.impl;

import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.StringUtil;
import com.gimi.cloud.jimi.common.utils.reqhead.Response;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.dto.prod.ProdBrandDTO;
import com.gimi.cloud.jimi.manage.consumer.feign.ProdBrandFeignService;
import com.gimi.cloud.jimi.manage.consumer.service.prod.BrandService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Service
public class BrandServiceImpl implements BrandService {
    private static final Logger logger = LoggerFactory.getLogger(BrandServiceImpl.class);
    private final ProdBrandFeignService prodBrandFeignService;

    @Autowired
    public BrandServiceImpl(ProdBrandFeignService prodBrandFeignService){
        this.prodBrandFeignService = prodBrandFeignService;
    }

    @Override
    public Response getBrandList(@RequestBody String requestBody, HttpServletRequest request, HttpServletResponse response) {
        ProdBrandDTO prodBrandDTO = JsonUtil.jsonToBean(requestBody,ProdBrandDTO.class);
        return prodBrandFeignService.getBrandList(prodBrandDTO);
    }

    @Override
    public Response addBrand(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        ProdBrandDTO prodBrandDTO = JsonUtil.jsonToBean(requestBody,ProdBrandDTO.class);
        if(StringUtil.isBlank(prodBrandDTO.getProdBrandDesk())||StringUtil.isBlank(prodBrandDTO.getProdBrandLogo())||
                StringUtil.isBlank(prodBrandDTO.getProdBrandName())|| StringUtil.isBlank(prodBrandDTO.getType())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return prodBrandFeignService.brandAdd(prodBrandDTO);
    }

    @Override
    public Response updateBrand(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        ProdBrandDTO prodBrandDTO = JsonUtil.jsonToBean(requestBody,ProdBrandDTO.class);
        if(StringUtil.isBlank(prodBrandDTO.getPdId())||StringUtil.isBlank(prodBrandDTO.getProdBrandDesk())||
                StringUtil.isBlank(prodBrandDTO.getProdBrandName())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return prodBrandFeignService.brandUpdate(prodBrandDTO);
    }

    @Override
    public Response deleteBrand(String requestBody, HttpServletRequest request, HttpServletResponse response) {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        ProdBrandDTO prodBrandDTO = JsonUtil.jsonToBean(requestBody,ProdBrandDTO.class);
        if(null!=prodBrandDTO && !StringUtil.isBlank(prodBrandDTO.getPdId()) && !StringUtil.isBlank(prodBrandDTO.getIsDelete())){
            return prodBrandFeignService.brandDelete(prodBrandDTO);
        }else{
            Response resp = new Response();
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR);
            return resp;
        }
    }

    @Override
    public Response getBrandPageList(String requestBody, HttpServletRequest request, HttpServletResponse response)  throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        ProdBrandDTO prodBrandDTO = JsonUtil.jsonToBean(requestBody,ProdBrandDTO.class);
        if(StringUtil.isBlank(prodBrandDTO.getPageSize())||StringUtil.isBlank(prodBrandDTO.getCurrentPage())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return prodBrandFeignService.getBrandPageList(prodBrandDTO);
    }

    @Override
    public Response gotoEditProdBrand(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        ProdBrandDTO prodBrandDTO = JsonUtil.jsonToBean(requestBody,ProdBrandDTO.class);
        if(StringUtil.isBlank(prodBrandDTO.getPdId())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return prodBrandFeignService.gotoEditProdBrand(prodBrandDTO);
    }

    @Override
    public Response fileUpload(HttpServletResponse response, HttpServletRequest request)  throws Exception{
        StandardMultipartHttpServletRequest multipartHttpServletRequest = (StandardMultipartHttpServletRequest) request;
        MultiValueMap multipartFiles = multipartHttpServletRequest.getMultiFileMap();

        return prodBrandFeignService.fileUpload(multipartFiles);

    }
}
